package com.chengqj.template;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: pattern
 * @description: mysql的数据库链接实现
 * @author: chengqj
 * @create: 2018-07-30 19:05
 **/
public class MysqlJDBCTemplate extends AbstractJdbcTemplate {

    @Override
    protected Connection getConnection() {
        System.out.println("mysql执行 parseResultSet");
        try {
            Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动
            String url = "jdbc:mysql://localhost:3306/demo";
            Connection conn = DriverManager.getConnection(url);
            return conn;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected List<?> parseResultSet(ResultSet rs) {
        System.out.println("mysql执行 parseResultSet");
        List<String> resultList = new ArrayList<>();
        try {
            while (rs.next()) {
                resultList.add(rs.getString("mysqlcolumnName"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultList;
    }

}
